package com.android.jr_infraredcamera.base

import android.content.Context
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.ViewTreeObserver
import android.widget.AdapterView
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.fragment.app.Fragment


/**
 * @author 战神族灬小火
 * @date 2021/12/23 16:42
 * @description
 */
abstract class BaseFragment<dataBinding : ViewDataBinding> constructor(layoutId: Int) : Fragment(),
    ViewTreeObserver.OnGlobalLayoutListener {

    private var layoutId = 0
    protected var bundle: Bundle? = null
    protected lateinit var binding: dataBinding
    protected var viewLoad: ViewLoad? = null

    companion object {
        private const val TAG = "===BaseFragment==="
    }

    init {
        this.layoutId = layoutId
    }


    override fun onAttach(context: Context) {
        super.onAttach(context)

    }


    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?,
    ): View? {

        binding = DataBindingUtil.inflate(inflater, layoutId, container, false)
        binding.lifecycleOwner = viewLifecycleOwner
        binding.root.addOnAttachStateChangeListener(object : View.OnAttachStateChangeListener {
            override fun onViewAttachedToWindow(v: View) {
                binding.root.viewTreeObserver?.addOnGlobalLayoutListener(this@BaseFragment)

            }

            override fun onViewDetachedFromWindow(v: View) {
                binding.root.viewTreeObserver?.removeOnGlobalLayoutListener(this@BaseFragment)

            }

        })

        fun fastClickChecked(view: View?) {
            view?.isClickable = false
            view?.postDelayed(kotlinx.coroutines.Runnable {
                view.isClickable = true
            }, 500)
        }


        return binding!!.root
    }

    override fun onGlobalLayout() {
        viewLoad?.load(true)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        initData()
        initView()
        initObservable()


    }

    protected fun setViewLoadListener(viewLoad: ViewLoad) {
        this.viewLoad = viewLoad
    }

    private fun unbindDrawables(view: View?) {
        if (view == null) {
            return
        }
        if (view.background != null) {
            view.background?.callback = null
        }
        if (view is ViewGroup && view !is AdapterView<*>) {
            for (i in 0 until view.childCount) {
                unbindDrawables(view.getChildAt(i))
            }
            view.removeAllViews()
        }
    }

    override fun onDestroyView() {
        unbindDrawables(view)
        binding.unbind()
        super.onDestroyView()
    }

    interface ViewLoad {
        fun load(state: Boolean)
    }

    override fun onResume() {
        super.onResume()
    }


    abstract fun initData()
    abstract fun initView()
    abstract fun initObservable()


}